<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/3/25
 * Time: 9:39
 */
namespace app\api\controller;
use think\Db;
class Shop extends Base
{
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }
    }

    /***
     * @return \think\response\Json
     * Banner图
     */
    public function banner(){
        $where['lunbo_type'] = 2;
        $where['lunbo_sta'] = 1;
        $res = Db::table('up_lunbo')->where($where)->order('lunbo_sort desc')->limit(5)->select();
        return  json(array('status'=>200,'msg'=>$res));
    }

    /***
     * @return \think\response\Json
     */
    public function assortment(){
        $where['assortment_status'] = 1;
        $where['assortment_del'] = 1;
        $res =  Db::table('up_goods_assortment')->where($where)->field('assortment_id,assortment_name')->select();
        return  json(array('status'=>200,'msg'=>$res));
    }

    /***
     * @return \think\response\Json
     * 首页
     */
    public function index(){
        $type = input('param.type')?input('param.type'):1;
        $page = input('param.page')?input('param.page'):1;
        $good_grade_id = input('param.good_grade_id');
        $good_type_id = input('param.good_type_id');
        $good_press_id = input('param.good_press_id');
        $num = 21;
        $limit = ($page-1)*$num;
        if($type==1){
            $where = array();
        }elseif ($type==2){
            $assortment_id = input('param.assortment_id');
            $where['good_assortment'] =$assortment_id;
        }
        if($good_grade_id!=''){
            $where['good_grade_id'] =$good_grade_id;
        }
        if($good_type_id!=''){
            $where['good_type_id'] =$good_type_id;
        }
        if($good_press_id!=''){
            $where['good_press_id'] =$good_press_id;
        }
        $where['good_status'] = 1;
        $res = Db::table('up_goods')
            ->where($where)
            ->field('good_assortment,good_name,good_id,good_image,good_price,good_type,good_y_price,good_thumb')
            ->limit($limit,$num)
            ->select();
        foreach ($res as $key=>&$vo){
            if($vo['good_type']==1){
              $spec_price =  Db::table('up_good_spec')->where('spec_good_id',$vo['good_id'])->value('spec_price');
              if($spec_price==''){
                  $vo['good_price'] = 0;
              }else{
                  $vo['good_price'] = $spec_price;
              }
            }
        }
        return  json(array('status'=>200,'msg'=>$res));
    }

    /***
     * @return \think\response\Json
     * 搜索
     */
    public function keyword(){
        $res = array();
        $keyword = input('param.keyword');
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        if($keyword!=''){
           $where['good_name|good_author|good_isbn'] = array('like',"%$keyword%");
        }else{
            return  json(array('status'=>200,'msg'=>$res));
        }
        $where['good_status'] = 1;
        $res = Db::table('up_goods')
            ->where($where)
            ->field('good_press,good_author,good_assortment,good_id,good_image,good_price,good_type,good_y_price,good_name, good_thumb')
            ->limit($limit,$num)
            ->select();
        foreach ($res as $key=>&$vo){
            if($vo['good_type']==1){
                $spec_price =  Db::table('up_good_spec')->where('spec_good_id',$vo['good_id'])->value('spec_price');
                if($spec_price==''){
                    $vo['good_price'] = 0;
                }else{
                    $vo['good_price'] = $spec_price;
                }
            }
        }
        return  json(array('status'=>200,'msg'=>$res));
    }

    /****
     * 收藏接口
     */
   public function like(){
       extract(input());
        $id = input('param.id');
       $where['like_user_id'] = $user_id;
       $where['like_shop_id'] = $id;
        $info = Db::table('up_my_like')->where($where)->find();
       if(!$info['like_id']){//证明没有收藏
           $data['like_user_id'] =$user_id;
           $data['like_shop_id'] =$id;
           $data['like_time'] = date('Y-m-d H:i:s',time());
           $res = Db::table('up_my_like')->insertGetId($data);
       }else{
           $res = Db::table('up_my_like')->where($where)->delete();
       }
       if($res){
           return  json(array('status'=>200,'msg'=>"成功"));
       }else{
           return  json(array('status'=>100,'msg'=>"失败"));
       }
   }
    /***
     * @return \think\response\Json
     * 详情
     */
    public function detail(){
        extract(input());
        $id = input('param.id');
//        $id = 60;
//        $user_id = 378;
        $field = 'good_weight,good_intro,good_assortment,good_id,good_name,good_image,good_price,good_author,good_press,good_isbn,good_stock,good_type,good_y_price,good_content,good_title,good_thumb,good_yi';
        //查询商品信息
        $res = Db::table('up_goods')->where('good_id',$id)->field($field)->find();
        if($res['good_type']==1){
            $spec_price =  Db::table('up_good_spec')->where('spec_good_id',$res['good_id'])->value('spec_price');
            $spec_stock =  Db::table('up_good_spec')
                ->where('spec_good_id',$res['good_id'])
                ->order('spec_stock DESC')
                ->value('spec_stock');
            $res['good_stock'] = 0;
            if($spec_stock){
                $res['good_stock'] = $spec_stock;
            }
            if($spec_price==''){
                $res['good_price'] = 0;
            }else{
                $res['good_price'] = $spec_price;
            }
        }
        $where['address_user_id'] = $user_id;
        $where['is_default'] = 2;
        $where['address_del'] = 1;
        $address_shi = Db::table('up_my_address')->where($where)->value('address_shi');
        $score = '不可配送';
        if($address_shi!=''){//证明有收货地址
            //查找运费价格
            $info = Db::table('up_area')->where('district_id',$address_shi)->find();
            if($info['status']==2){//证明该地区不可以配送
                $res['is_score'] = 2;//不可以配送
            }else{
                if($info['price']==''||$info['price']==null){
                    $score = Db::table('up_sign_config')->where('id',2)->value('score');
                }else{
                    $score = $info['price'];
                }
                $res['is_score'] = 1;//可以配送
            }

        }else{//没有收货地址
            $user_shi = Db::table('up_u_user')->where('user_id',$user_id)->value('user_shi');
            if($user_shi==''||$user_shi==null){//没有数据
                $score = Db::table('up_sign_config')->where('id',2)->value('score');
                $res['is_score'] = 3;//未知
            }else{//证明有数据去查找属于的市的运费
                $info = Db::table('up_area')->where('district_id',$user_shi)->find();
                if($info['status']==2){//证明该地区不可以配送
                    $res['is_score'] = 2;//不可以配送
                }else{
                    $res['is_score'] = 1;//可以配送
                }
                $score = $info['price'];
            }
        }

        $res['score'] = $score;
        $isLike = Db::table('up_my_like')
            ->where('like_shop_id', $id)
            ->where('like_user_id', $user_id)
            ->find();
        if($isLike){
            $res['like'] = 1;
        }else{
            $res['like'] = 0;
        }
        Db::table('up_goods')->where('good_id',$id)->setInc('good_visitor',1);
        return  json(array('status'=>200,'msg'=>$res));
    }
    /***
     * @return \think\response\Json
     * 商品规格接口
     */
    public function spec(){
        $id = input('param.id');
        $res = Db::table('up_good_spec')->where('spec_good_id',$id)->select();
        return  json(array('status'=>200,'msg'=>$res));
    }

    /***
     * @return \think\response\Json
     * 商品参数接口
     */
    public function parameter(){
        $id = input('param.id');
        $res = Db::table('up_good_parameter')->where('parameter_good_id',$id)->select();
        return  json(array('status'=>200,'msg'=>$res));
    }

    /***
     * @return \think\response\Json
     * 添加购物车
     */
    public function  promptly(){
        extract(input());
        $id = input('param.id');
        $num = input('param.num');
        $spec_id = input('param.spec_id');
        $good_assortment = input('param.good_assortment');
//        if($spec_id!=''){
            if($spec_id!=''){//证明是非书籍
                $where['car_spec_id'] = $spec_id;
                //查询商品的库存
                $good_stock = Db::table('up_good_spec')->where('spec_id',$spec_id)->value('spec_stock');
            }else{
                //查询商品的库存
                $good_stock = Db::table('up_goods')->where('good_id',$id)->value('good_stock');
            }
            if($good_stock>100){
                $kc_num = 100;
                $msg = '不能超过上限100';
            }else{
                $kc_num = $good_stock;
                $msg = '不能超过商品库存';
            }

            $where['car_goodid'] = $id;
            $where['car_userid'] = $user_id;
            $info = Db::table('up_shop_car')->where($where)->find();
            if($info['car_id']!=''){//购物车存在
                //todo  新加的
                return  json(array('status'=>100,'msg'=>"购物车已经有此商品"));

                if(($num+$info['car_number'])>$kc_num){
                    return  json(array('status'=>100,'msg'=>$msg));
                }
                $data1['car_time'] = date('Y-m-d H:i:s',time());
                $data1['car_number'] = $num+$info['car_number'];
                $data1['car_assortment'] = $good_assortment;
                Db::startTrans();
                $res = Db::table('up_shop_car')->where($where)->update($data1);
                if($res){
                    Db::commit();
                    return  json(array('status'=>200,'msg'=>$res));
                }else{
                    Db::rollback();//回滚
                    return  json(array('status'=>100,'msg'=>"失败"));
                }
            }else{//购物车不存在
                if($num>$good_stock){
                    return  json(array('status'=>100,'msg'=>"不能超过商品库存"));
                }
                $data['car_userid'] = $user_id;
                $data['car_goodid'] = $id;
                $data['car_number'] = $num;
                $data['car_spec_id'] = $spec_id;
                $data['car_assortment'] = $good_assortment;
                if($spec_id!=''){
                    $data['car_is_book'] = 1;
                }else{
                    $data['car_is_book'] = 2;
                }
                $data['car_time'] = date('Y-m-d H:i:s',time());
                $res = Db::table('up_shop_car')->insertGetId($data);
                if($res){
                    return  json(array('status'=>200,'msg'=>$res));
                }else{
                    return  json(array('status'=>100,'msg'=>"失败"));
                }
            }
//        }
    }
    /***
     * @return \think\response\Json
     * 立即购买
     */
    public function  promptlys(){
             extract(input());
            $id = input('param.id');
            $num = input('param.num');
            $spec_id = input('param.spec_id');
            $good_assortment = input('param.good_assortment');
            if($spec_id!=''){//证明是非书籍
                $where['car_spec_id'] = $spec_id;
                //查询商品的库存
//                $good_stock = Db::table('up_good_spec')->where('spec_id',$spec_id)->value('spec_stock');
            }
//            else{
//                //查询商品的库存
//                $good_stock = Db::table('up_goods')->where('good_id',$id)->value('good_stock');
//            }
//            $where['car_goodid'] = $id;
//            $where['car_userid'] = $user_id;
//            $info = Db::table('up_shop_car')->where($where)->find();
//            if(($num+$info['car_number'])>$good_stock){
//                return  json(array('status'=>100,'msg'=>"不能超过商品库存"));
//            }
            $data['car_userid'] = $user_id;
            $data['car_goodid'] = $id;
            $data['car_number'] = $num;
            $data['car_spec_id'] = $spec_id;
            $data['car_assortment'] = $good_assortment;

            if($spec_id!=''){
                $data['car_is_book'] = 1;
            }else{
                $data['car_is_book'] = 2;
            }
            $data['car_time'] = date('Y-m-d H:i:s',time());
            $res = Db::table('up_shop_car')->insertGetId($data);
            if($res){
                return  json(array('status'=>200,'msg'=>$res));
            }else{
                return  json(array('status'=>100,'msg'=>"失败"));
            }
    }

    /***
     * @return \think\response\Json
     * 立即购买,不支付返回修改购物车信息
     */
    public function  backCar(){
        extract(input());
        $id = input('param.car_id');
        $info = Db::table('up_shop_car')->where('car_id',$id)->find();
        if($info['car_is_book']==1){
            $where['car_spec_id'] = $info['car_spec_id'];
        }
           $where['car_userid'] = $user_id;
           $where['car_goodid'] = $info['car_goodid'];
           $where['car_id'] = array('neq',$id);
        //查询这个用户是否购买了相同的商品
           $info1 = Db::table('up_shop_car')->where($where)->find();
           if($info1['car_is_book']==1){
              $good_stock = Db::table('up_good_spec')->where('spec_id',$info1['car_spec_id'])->value('spec_stock');

           }else{
              $good_stock = Db::table('up_goods')->where('good_id',$info1['car_goodid'])->value('good_stock');

           }
        if($info1['car_id']!=''){//存在这个用户购买了相同的商品
            $num = $info1['car_number']+$info['car_number'];
            if($good_stock>100){
                $kc_num = 100;
            }else{
                $kc_num = $good_stock;
            }
            if(($info1['car_number']+$info['car_number'])>$kc_num){//查询大于库存或者100
                $number =  $kc_num;
            }else{
                $number = $num;
            }

           Db::table('up_shop_car')->where('car_id',$info1['car_id'])->setField('car_number',$number);
           Db::table('up_shop_car')->where('car_id',$info['car_id'])->delete();
        }
            return  json(array('status'=>200,'msg'=>"成功"));

    }

    /***
     * @return \think\response\Json
     * 商品详情/
     */
    public function orderDetail(){
        //todo   没有更新到接口文档
        extract(input());
        $car_id = input('param.car_id');
        $where['car_id'] = array('IN',$car_id);
        $field = 'ug.good_press,a.car_assortment,a.car_is_book,a.car_number,a.car_spec_id,a.car_goodid,ug.good_image,ug.good_price,ug.good_y_price,ug.good_type,ug.good_author,ug.good_name,ug.good_thumb';
        $res = Db::table('up_shop_car')
            ->alias('a')
            ->join('up_goods ug','ug.good_id=a.car_goodid','left')
            ->field($field)
//            ->limit($limit*$num,$num)
            ->where($where)
            ->select();
        $is_a= 2;//非人民币支付
        $is_b= 2;//非喵币支付
        foreach ($res as $key=>&$vo){
            if($vo['car_spec_id']!=''){
                $spec_price = Db::table('up_good_spec')->where('spec_id',$vo['car_spec_id'])->value('spec_price');
                if($spec_price==''){
                    $vo['good_price'] = 0;
                }else{
                    $vo['good_price'] = $spec_price;
                }
            }
            if($vo['car_is_book']==1){//非书籍
                $is_a =1;//
            }elseif ($vo['car_is_book']==2){//书籍
                $is_b =1;//喵币支付
            }
        }
        if($is_a==1&&$is_b!=1){
            $order_hh_payment = 3;//人民币支付
        }
        if($is_a!=1&&$is_b==1){
            $order_hh_payment = 2;//喵币支付
        }
        if($is_a==1&&$is_b==1){
            $order_hh_payment = 1;//混合支付
        }
        return  json(array('status'=>200,'msg'=>$res,'order_hh_payment'=>$order_hh_payment));

    }

    /***
     * @return \think\response\Json
     * 我的默认收货地址
     */
    public function address(){
        extract(input());
        $where['address_user_id'] = $user_id;
        $where['is_default'] = 2;
        $where['address_del'] = 1;
        //查找默认收货地址
        $res = Db::table('up_my_address')->alias('a')->where($where)
            ->join('up_area ua','ua.district_id = a.address_sheng','left')
            ->join('up_area ua1','ua1.district_id = a.address_shi','left')
            ->join('up_area ua2','ua2.district_id = a.address_qu','left')
            ->field('a.address_name,a.address_id,a.address_detail,a.address_phone
            ,ua.district,ua1.district as district1,ua2.district as district2')->find();
        if(!$res){
            return  json(array('status'=>200,'msg'=>[]));
        }
        //--------------------额外加的超重的费用 start -------------------------------------
        $over = 0;
        if(isset($good_id) && $good_id != ''){
            $over = $this->getOverYunFei($good_id,1);
        }
        if(isset($car_id) && $car_id != ''){
            $over = $this->getOverYunFei($car_id,2);
        }
        //--------------------额外加的超重的费用 end -------------------------------------
        //查询我
        $where1['address_user_id'] = $user_id;
        $where1['is_default'] = 2;
        $where1['address_del'] = 1;
        $address_shi = Db::table('up_my_address')->where($where1)->value('address_shi');
        $info = Db::table('up_area')->where('district_id',$address_shi)->find();
        $score = '不可配送';
        //查找运费价格
//        $info = Db::table('up_area')->where('district_id',$address_shi)->find();
        if($info['status']==2){//证明该地区不可以配送
            $res['is_score'] = 2;//不可以配送
        }else{
            if($info['price']==''||$info['price']==null){
                $score = Db::table('up_sign_config')->where('id',2)->value('score');
            }else{
                $score = $info['price'];
            }
            $res['is_score'] = 1;//可以配送
        }
//        if($info['price']==''||$info['price']==null){
//            $score = Db::table('up_sign_config')->where('id',2)->value('score');
//        }else{
//            $score = $info['price'];
//        }
        $res['score'] = $score + $over;
        if($info['status']==2){//证明该地区不可以配送
            $res = array();//不可以配送
        }
        return  json(array('status'=>200,'msg'=>$res));
    }
    //选择地址
    public function freight(){
        extract(input());
        $where['address_id'] = input('param.id');
        //查找默认收货地址
//        $res = Db::table('up_my_address')->alias('a')->where($where)
//            ->join('up_area ua','ua.district_id = a.address_sheng','left')
//            ->join('up_area ua1','ua1.district_id = a.address_shi','left')
//            ->join('up_area ua2','ua2.district_id = a.address_qu','left')
//            ->field('a.address_name,a.address_id,a.address_detail,a.address_phone
//            ,ua.district,ua1.district as district1,ua2.district as district2')->find();
        $res = array();
        //查询我
        $address_shi = Db::table('up_my_address')->where($where)->value('address_shi');
        $info = Db::table('up_area')->where('district_id',$address_shi)->find();
        //查找运费价格
//        $info = Db::table('up_area')->where('district_id',$address_shi)->find();
        if($info['status']==2){//证明该地区不可以配送
            $res['is_score'] = 2;//不可以配送
            $score = '不可配送';
        }else{
            if($info['price']==''||$info['price']==null){
                $score = Db::table('up_sign_config')->where('id',2)->value('score');
            }else{
                $score = $info['price'];
            }
            $res['is_score'] = 1;//可以配送
        }
        //--------------------额外加的超重的费用 start -------------------------------------
        $over = 0;
        if(isset($good_id) && $good_id != ''){
            $over = $this->getOverYunFei($good_id,1);
        }
        if(isset($car_id) && $car_id != ''){
            $over = $this->getOverYunFei($car_id,2);
        }
        //--------------------额外加的超重的费用 end -------------------------------------
        $res['score'] = $score + $over;
        return  json(array('status'=>200,'msg'=>$res));
    }
    /***
     * @return \think\response\Json
     * 我的收货地址
     */
    public function myAddress(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $where['address_user_id'] = $user_id;
        $where['address_del'] = 1;
        $num = 10;
        $limit = $page-1;
        $res = Db::table('up_my_address')->alias('a')->where($where)
            ->join('up_area ua','ua.district_id = a.address_sheng','left')
            ->join('up_area ua1','ua1.district_id = a.address_shi','left')
            ->join('up_area ua2','ua2.district_id = a.address_qu','left')
            ->field('a.address_name,a.address_id,a.address_detail,a.address_phone
            ,ua.district,ua1.district as district1,ua2.district as district2')
            ->field('a.address_id,a.address_detail,a.address_phone,a.is_default,a.address_name
            ,ua.district,ua1.district as district1,ua2.district as district2,ua1.status')
            ->limit($limit*$num,$num)
            ->order('a.address_time desc')
            ->select();
        return  json(array('status'=>200,'msg'=>$res));
    }

    /**
     * @return \think\response\Json
     * 修改默认地址状态
     */
    public function defaultAddress(){
        extract(input());
        $id = input('param.address_id');
        Db::startTrans();
        $res = Db::table('up_my_address')->where('address_id',$id)->setField('is_default',2);
        $where['address_user_id'] = $user_id;
        $where['address_id'] = array('neq',$id);
        $num = Db::table('up_my_address')->where($where)->count();
        if($num>1){
            Db::table('up_my_address')->where($where)->setField('is_default',1);
        }
        if($res){
            Db::commit();
            return  json(array('status'=>200,'msg'=>"成功"));
        }else{
            Db::rollback();//回滚
            return  json(array('status'=>100,'msg'=>"失败"));
        }
    }
    /***
     * @return \think\response\Json
     * 添加收货地址
     */
    public function added(){
        extract(input());
        $data['address_name'] = input('param.address_name');
        $data['address_phone'] = input('param.address_phone');
        $data['address_sheng'] = input('param.address_sheng');
        $data['address_shi'] = input('param.address_shi');
        $data['address_qu'] = input('param.address_qu');
        $data['address_detail'] = input('param.address_detail');
        $data['is_default'] = input('param.is_default');
        $data['address_user_id'] = $user_id;
        $data['address_time'] = date('Y-m-d H:i:s',time());
        $data['address_update_time'] = date('Y-m-d H:i:s',time());
        $res = Db::table('up_my_address')->insertGetId($data);
       if($res){
           return  json(array('status'=>200,'msg'=>"成功"));
       }else{
           return  json(array('status'=>100,'msg'=>"失败"));
       }
    }

    /***
     * @return \think\response\Json
     * 购物车
     */
    public function car(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $where['car_userid'] =$user_id;
        $field = 'a.car_assortment,a.car_id,a.car_number,a.car_spec_id,a.car_goodid,ug.good_status,
        ug.good_image,ug.good_price,ug.good_y_price,ug.good_type,ug.good_name,ug.good_stock,ug.good_thumb';
        $res = Db::table('up_shop_car')
            ->alias('a')
            ->join('up_goods ug','ug.good_id=a.car_goodid','left')
            ->field($field)
            ->limit($limit*$num,$num)
            ->where($where)
            ->order('a.car_id desc')
            ->select();
        foreach ($res as $key=>&$vo){
            if($vo['car_spec_id']!=''){//证明是非图书
                $spec_price = Db::table('up_good_spec')->where('spec_id',$vo['car_spec_id'])->find();
                if($spec_price['spec_id']!=''){
                    $vo['good_price'] = $spec_price['spec_price'];
                    $vo['good_stock'] = $spec_price['spec_stock'];
                }else{
                    $vo['good_price'] = 0;
                    $vo['good_stock'] = '';
                }
                if($vo['good_status']==2||$vo['good_stock']<1){//证明本商品不能购买
                    $vo['status']=1;
                }else{
                    $vo['status']=2;
                }
            }else{//图书
                if($vo['good_status']==2||$vo['good_stock']<1){//证明本商品不能购买
                    $vo['status']=1;
                }else{
                    $vo['status']=2;
                }
            }
            unset($res[$key]['good_status']);
        }

        return  json(array('status'=>200,'msg'=>$res));
    }

    /***
     * @return \think\response\Json
     * 删除购物车
     */
    public function delCar(){
        extract(input());
        $car_id = input('param.car_id');
        $where['car_id'] = array('IN',$car_id);
        $info = Db::table('up_shop_car')->where($where)->find();
        if($user_id!=$info['car_userid']){
            return  json(array('status'=>100,'msg'=>"不能删除不属于自己购物车信息"));
        }
        $res = Db::table('up_shop_car')->where($where)->delete();
        if($res){
            return  json(array('status'=>200,'msg'=>"删除成功"));
        }else{
            return  json(array('status'=>100,'msg'=>"删除失败"));
        }
    }

    /***
     * @return \think\response\Json
     * 购物车列表修改数量
     */
    public function shopUpdate(){
        extract(input());
        $car_id = explode(',',input('param.car_id'));
        $num = explode(',',input('param.num'));
        foreach ($car_id as $key=>$vo){
           Db::table('up_shop_car')->where('car_id',$vo)->setField('car_number',$num[$key]);
        }
            return  json(array('status'=>200,'msg'=>"成功"));
    }

    /***
     * @return \think\response\Json
     * 优惠卷
     */
    public function coupon(){
        extract(input());
        $this->timeoutForCoupon($user_id);
        $car_id = input('param.car_id');
//        $car_id = 224;
//        $user_id = 40;
        $map['order_userid'] = $user_id;
        $map['order_status'] = 5;
        $map['order_is_coupon'] = 1;
        $map['order_timeout_coupon'] = 1;
        $result = Db::table('up_order')->where($map)->field('order_id,order_is_coupon,order_my_coupon')->select();
        if($result){
            foreach ($result as $key=>$vo){
                Db::table('up_my_coupon')->where('mc_id',$vo['order_my_coupon'])->setField('mc_status',1);
                Db::table('up_order')->where('order_id',$vo['order_id'])->setField('order_timeout_coupon',2);
            }
        }
        if($car_id!=''){
         $where['car_id']  = array('IN',$car_id);
        }
        $where['car_userid'] = $user_id;
        //查询购物车
        $info = Db::table('up_shop_car')->where($where)->select();
        $coupon_use = implode(',',array_unique(array_column($info,'car_is_book')));
        $car_goodid = implode(',',array_column($info,'car_goodid'));
        $coupon_assortment = implode(',',array_unique(array_column($info,'car_assortment')));

        $coupon_use1 = array_unique(array_column($info,'car_is_book'));
        $time = date('Y-m-d H:i:s',time());
//        print_r($info);die;
        $where1['a.mc_user_id'] = $user_id;
        $where1['a.mc_status'] = 1;
        $where1['uc.coupon_status'] = 1;
        $where1['uc.coupon_online'] = 1;
        $where1['uc.coupon_use'] = array('IN',$coupon_use);
        $where1['uc.coupon_start'] = array('<=',$time);
        $where1['uc.coupon_end'] = array('>=',$time);
        $where2['coupon_good_id'] = array('IN',$car_goodid);
//        $where2['coupon_is_zd'] = 2;
//        $where2['uc.coupon_good_id'] = array('neq',null);
        $where3['coupon_assortment'] = array('IN',$coupon_assortment);
//        $where3['coupon_is_zd'] = 1;
//        $where3['uc.coupon_assortment'] = array('neq',null);
//        die;
        $where4['coupon_all'] = array('IN',array(1,2));
        $where5['coupon_all'] = 1;
        $where6['coupon_all'] = 2;
        $where1['a.mc_type'] = 1;
        if(count($coupon_use1)>=2){//查全部
            $res  = Db::table('up_my_coupon')->alias('a')
                ->join('up_coupon uc','uc.coupon_id = a.mc_coupon_id','LEFT')
                ->field('uc.coupon_is_zd,uc.coupon_assortment,uc.coupon_content,uc.coupon_start,uc.coupon_end,a.mc_id,a.mc_coupon_id,uc.coupon_name,uc.coupon_use,uc.coupon_price,uc.coupon_good_id')
                ->where($where1)
//            ->where(function ($query) use ($where2){
//                $query->where('coupon_is_zd',2)
//                    ->where($where2);
//            })
//            ->whereOr(function ($query) use ($where3) {
//                $query->where('coupon_is_zd',1)
//                    ->where($where3);
//            })
                ->where(function ($q) use ($where2, $where3,$where4){
                    $q->where($where2)
                        ->whereOr($where3)
                        ->whereOr($where4);
                })
//            ->fetchSql(true)
                ->select();
        }else{
            if($coupon_use1[0]==1){//非图书
                $res  = Db::table('up_my_coupon')->alias('a')
                    ->join('up_coupon uc','uc.coupon_id = a.mc_coupon_id','LEFT')
                    ->field('uc.coupon_is_zd,uc.coupon_assortment,uc.coupon_content,uc.coupon_start,uc.coupon_end,a.mc_id,a.mc_coupon_id,uc.coupon_name,uc.coupon_use,uc.coupon_price,uc.coupon_good_id')
                    ->where($where1)
                    ->where(function ($q) use ($where2, $where3,$where5){
                        $q->where($where2)
                            ->whereOr($where3)
                            ->whereOr($where5);
                    })
//            ->fetchSql(true)
                    ->select();
            }else{//图书
                $res  = Db::table('up_my_coupon')->alias('a')
                    ->join('up_coupon uc','uc.coupon_id = a.mc_coupon_id','LEFT')
                    ->field('uc.coupon_is_zd,uc.coupon_assortment,uc.coupon_content,uc.coupon_start,uc.coupon_end,a.mc_id,a.mc_coupon_id,uc.coupon_name,uc.coupon_use,uc.coupon_price,uc.coupon_good_id')
                    ->where($where1)
                    ->where(function ($q) use ($where2, $where3,$where6){
                        $q->where($where2)
                            ->whereOr($where3)
                            ->whereOr($where6);
                    })
//            ->fetchSql(true)
                    ->select();
            }
        }

//        echo $res;

        return  json(array('status'=>200,'msg'=>$res));
    }
    protected function timeoutForCoupon($user_id)
    {
        $where['order_userid'] = $user_id;//我的
        $time = date('Y-m-d H:i:s', strtotime('-30 minute'));
        $where['order_time'] = ['<=', $time];//下单时间为半个小时之前的
        $where['order_del'] = 1;
        $where['order_status'] = 1;//未支付
        $overOrder_ids = Db::table('up_order')
            ->where($where)
            ->column('order_id');
        Db::table('up_order')->where('order_id', 'IN', $overOrder_ids)->setField('order_status', 5);
        Db::table('up_order')->where('order_id', 'IN', $overOrder_ids)->setField('order_timeout_coupon', 1);
        return 1;
    }
    /***
     * @return \think\response\Json
     * 取消订单
     */
    public function cancel(){
        $id = input('param.id');
        $res = Db::table('up_order')->where('order_id',$id)->setField('order_status',6);
        if($res){
            return  json(array('status'=>200,'msg'=>"成功"));
        }else{
            return  json(array('status'=>100,'msg'=>"失败"));
        }
    }
    /***
     * @return \think\response\Json
     * 订单详情
     */
    public function orderXq(){
        $id = input('param.id');
        $field ='order_sendtime,order_user_pay,order_j_glod,order_sheng,order_shi,order_qu,order_status,order_id,order_number,order_name,order_address,
                 order_phone,order_all_price,order_glod,order_freight_money,
                 order_all_price,order_hh_payment,order_is_coupon,order_my_coupon,
                 order_time,order_paytime,order_type,order_suretime,order_express_name,order_express_num';
        //查询订单信息
        $where['order_status'] = array('neq',6);
        $where['order_id'] = $id;
        $res = Db::table('up_order')->where($where)->field($field)->find();
        if($res['order_is_coupon']==1){//使用优惠卷,查询优惠卷价格
            $info = Db::table('up_my_coupon')->alias('a')
                ->join('up_coupon uc','uc.coupon_id = a.mc_coupon_id','left')
                ->field('uc.coupon_price,uc.coupon_use')
                ->where('a.mc_id',$res['order_my_coupon'])
                ->find();
            $res['coupon_price'] = $info['coupon_price'];
            $res['coupon_use'] = $info['coupon_use'];
        }
        $orderInfo = Db::table('up_order_info')->where('oi_order_id',$res['order_id'])->select();
//        foreach ($orderInfo as $key=>&$vo){//循环订单详情数据
//                if($vo['oi_spec_id']!=''||$vo['oi_spec_id']!=null){//非图书
//                    Db::table('up_good_spec')
//                }
//        }
        $district =Db::table('up_area')->where('district_id',$res['order_sheng'])->value('district');
        $district1 =Db::table('up_area')->where('district_id',$res['order_shi'])->value('district');
        $district2 =Db::table('up_area')->where('district_id',$res['order_qu'])->value('district');
        $res['orderInfo'] = $orderInfo;
        $res['district'] = $district;
        $res['district1'] = $district1;
        $res['district2'] = $district2;
        return  json(array('status'=>200,'msg'=>$res));
    }

    /***
     * @return \think\response\Json
     * 订单详情
     */
    public function postOrder(){
        extract(input());
        $order_number = date('YmdHis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);//订单编号

        $car_id  = input('param.car_id');//购物车id
        $count1 = Db::table('up_shop_car')->where('car_id','in',"$car_id")->select();
        if(count($count1)>=2){
            return json_encode(['status' => 0, 'msg' => '抱歉,您选购商品品种不能超过2个']);
        }else{
            if($count1[0]['car_number'] > 1){
                return json_encode(['status' => 0, 'msg' => '抱歉,您选购商品数量不能超过1个']);
            }
        }




        $address_id  = input('param.address_id');//默认收货地址ID
        $is_coupon  = input('param.is_coupon');//是否使用优惠卷  1使用优惠卷  2没有使用优惠卷
        $mc_id  = input('param.mc_id');//优惠卷ID
        $order_freight_money  = input('param.order_freight_money');//运费
        $order_all_price  = input('param.order_all_price');//总人民币(商品的总价格)
        $order_glod  = input('param.order_glod');//总喵币(商品的总喵币)
        $order_user_pay  = input('param.order_user_pay');//要支付的人民币(商品价格+运费)
        $order_type  = input('param.order_type');//1是支付宝  2是微信
        $order_j_glod  = input('param.order_j_glod');//减去的喵币(如果使用优惠卷减去的价格)
        // 启动事务
        Db::startTrans();
        try{
            if($is_coupon==1){
                Db::table('up_my_coupon')->where('mc_id',$mc_id)->setField('mc_status',2);
            }
//            $car_id= 1223;
//			$address_id = 33;
//            $is_coupon = 2;
//            $mc_id = '';
//            $order_freight_money = 22;
//            $order_all_price =  22;
//            $order_glod =  0;
//            $order_j_glod = 0;
//            $order_user_pay =  22;
//             $order_type = 1;
//             $user_id = 152;

            //查询收货地址情况
            $addressInfo = Db::table('up_my_address')->alias('a')
                ->join('up_area ua','ua.district_id = a.address_sheng')
                ->join('up_area ua1','ua1.district_id = a.address_shi')
                ->join('up_area ua2','ua2.district_id = a.address_qu')
                ->field('a.address_telephone,a.address_sheng,a.address_shi,a.address_qu,a.address_name,a.address_phone,ua.district,ua1.district as district1,ua2.district as district2,a.address_detail')
                ->where('address_id',$address_id)->find();

            //查询购物情况
            $where['car_id'] = array('IN',$car_id);
            $where['car_userid'] = $user_id;
            $res = Db::table('up_shop_car')->where($where)->select();
            //结束
            $shop_price = 0;
            $num = 0;
            $is_a= 2;//非人民币支付
            $is_b= 2;//非喵币支付

            foreach ($res as $key=>$vo){
                $result = Db::table('up_goods')->where('good_id',$vo['car_goodid'])->find();
                if($vo['car_is_book']==2){//图书
                    if($vo['car_number']>$result['good_stock']){
                        return json_encode(['status' => 0,  'msg' => '抱歉,您选购的'.$result['good_name'].'数量大于剩余库存数量,请重新选择']);
                    }
                }elseif ($vo['car_is_book']==1){//非图书
                    $result1 = Db::table('up_good_spec')->where('spec_id',$vo['car_spec_id'])->find();
                    if($vo['car_number']>$result1['spec_stock']){
                        return json_encode(['status' => 0, 'msg' => '抱歉,您选购的'.$result['good_name'].'商品暂时无货']);
                    }
                }
                $num += $vo['car_number'];//总数量
                //查询这个商品的价格
                $goodInfo = Db::table('up_goods')
                    ->where('good_id',$vo['car_goodid'])
                    ->find();
                /***************插入订单详情开始*************/
                if($vo['car_is_book']==1){//非书籍
                    $goodSpec = Db::table('up_good_spec')->where('spec_id',$vo['car_spec_id'])->find();
                    if($goodSpec['spec_id']==''){
                        $shop_price = 0;
                    }else{
                        $shop_price = $goodSpec['spec_price'];
                    }
                    $oi_type = 1;//人民币支付
                    $is_a =1;//
                }elseif ($vo['car_is_book']==2){//书籍
                    $shop_price = $goodInfo['good_y_price'];//喵币价格
                    $oi_type =2;//喵币支付
                    $is_b =1;//喵币支付
                }
                $dataInfo['oi_spec_id'] = $vo['car_spec_id'];//非书籍规格ID
                $dataInfo['oi_good_id'] = $vo['car_goodid'];//商品ID
                $dataInfo['oi_price'] = $shop_price;//购买商品的单价格
                $dataInfo['oi_num'] = $vo['car_number'];//购物数量
                $dataInfo['oi_good_name'] = $goodInfo['good_name'];
                $dataInfo['oi_good_thumb'] = $goodInfo['good_thumb'];
                $dataInfo['oi_pay_time'] = date('Y-m-d H:i:s',time());
                $dataInfo['oi_good_coding'] = $goodInfo['good_coding'];
                $dataInfo['oi_type'] = $oi_type;
                $dataInfo['oi_user_id'] = $user_id;
                //返回ID
                $order_info_id = Db::table('up_order_info')->insertGetId($dataInfo);
                $dataList[]=$order_info_id;
            }
            if($is_a==1&&$is_b!=1){
                $order_hh_payment = 3;//人民币支付
            }
            if($is_a!=1&&$is_b==1){
                $order_hh_payment = 2;//喵币支付
            }
            if($is_a==1&&$is_b==1){
                $order_hh_payment = 1;//混合支付
            }
            $data['order_number'] = $order_number;//订单编号
            $data['order_userid'] = $user_id;//购物人
            $data['order_num'] = $num;//数量
            $data['order_user_pay'] = $order_user_pay;//支付的人民币价格
            $data['order_type'] = $order_type;//1是支付宝  2是微信  3喵币支付
            $data['order_time'] = date('Y-m-d H:i:s',time());//下单时间
            $data['order_my_address'] = $addressInfo['address_detail'];//收货地址
            $data['order_address'] = $addressInfo['address_detail'];//收货地址
            $data['order_sheng'] = $addressInfo['address_sheng'];//收货省
            $data['order_shi'] = $addressInfo['address_shi'];//收货市
            $data['order_qu'] = $addressInfo['address_qu'];//收货区
            $data['order_name'] = $addressInfo['address_name'];//收货人
            $data['order_phone'] = $addressInfo['address_phone'];//收货人手机号码
            $data['order_hh_payment'] = $order_hh_payment;//1混合支付  2喵币支付  3人民币支付
            $data['order_is_coupon'] = $is_coupon;//1使用优惠卷  2没有使用优惠卷
            $data['order_j_glod'] = $order_j_glod;//优惠卷减去的喵币
            $data['order_telphone'] = $addressInfo['address_phone']?$addressInfo['address_phone']:$addressInfo['address_telephone'];//优惠卷减去的喵币
            if($is_coupon==1){
                $data['order_my_coupon'] = $mc_id;
            }
            $data['order_freight_money'] = $order_freight_money;//运费
            $data['order_all_price'] = $order_all_price;//总的人民币
            $data['order_glod'] = $order_glod;//总的喵币
            $result = Db::table('up_order')->insertGetId($data);//插入订单表
            //order_info查询条件
            $where1['oi_id'] = array('IN',implode(',',$dataList));
            /*****************结束******************/

            /*********************结束***********************************/
            $data1['oi_order_id'] = $result;
            $aa = Db::table('up_order_info')->where($where1)->update($data1);
            $where2['car_id'] = array('IN',$car_id);
            $bb = Db::table('up_shop_car')->where($where2)->delete();
            Db::commit();
            return  json(array('status'=>200,'msg'=>$result));
        }catch(\Exception $e){
            Db::rollback();//回滚
            return  json(array('status'=>100,'msg'=>"提交订单失败"));
        }
    }

    /**
     * @return string
     * 喵币支付
     */
    public function paymentGold(){
        extract(input());
        $params = input('param.');
//        $params['id'] = 250;
//        $params['id'] = 849;
//      $user_id = 1400000;
        //查询这个订单详情
        $info = Db::table('up_order')->where('order_id',$params['id'])->find();
        if($info['order_id']==''){
            return json_encode(['status' => 0, 'msg' => '订单不存在']);
        }

        $num = Db::table('up_convert')->where('convert_id',1)->find();
        $start = date('Y-m-d H:i:s',strtotime('-24 hours'));
        $end = date('Y-m-d H:i:s',time());
        //查询当前时间24小时之前购物的商品记录
        $map['order_time'] = array('between',array($start,$end));
        $map['order_userid'] = $user_id;
        $map['order_hh_payment'] = array('IN','1,2');
        $map['order_status'] = array('IN','2,3,4');
        $count = Db::table('up_order')->where($map)->count();
        if($count>=$num['convert_num']){
            return json_encode(['status'=>0,'msg'=>"规定时间内兑换喵币次数超过默认限制"]);
        }
        //查询订单里面每个商品的详情
        $goodInfo  = Db::table('up_order_info')->where('oi_order_id',$params['id'])->select();

        //判断库存数量
        foreach ($goodInfo as $key=>$vo){
            $result = Db::table('up_goods')->where('good_id',$vo['oi_good_id'])->find();
                if($vo['oi_num']>$result['good_stock']){
                    return json_encode(['status' => 0,'msg' => '抱歉,您选购的'.$result['good_name'].'数量大于剩余库存数量,请重新选择']);
                }
        }
        Db::startTrans();
        try{
            $this->getHonorForEvery($info['order_userid'],10);
            //查询这个用户的喵币
            $user_integral = Db::table('up_u_user')->where('user_id',$user_id)->value('user_integral');
            if($info['order_j_glod']>$user_integral){//证明购买花费的喵币大于用户账号的喵币数量
                return json_encode(['status'=>100,'msg'=>"喵币不足不能购买"]);
            }else{
                //修改用户账户的余额
                $aa = Db::table('up_u_user')->where('user_id',$user_id)->setDec('user_integral',$info['order_j_glod']);
                $resultAll['glod_user_id'] =$user_id;
                $resultAll['glod_status'] =5;
                $resultAll['glod_type'] =1;
                $resultAll['glod_money'] =$info['order_j_glod'];
                $resultAll['glod_time'] =date('Y-m-d H:i:s',time());
                Db::table('up_glod')->insertGetId($resultAll);
//            if($aa){
                foreach ($goodInfo as $key=>$vo){
                    $bb = Db::table('up_goods')->where('good_id',$vo['oi_good_id'])->setDec('good_stock',$vo['oi_num']);
                    Db::table('up_goods')->where('good_id',$vo['oi_good_id'])->setInc('good_sales_num',$vo['oi_num']);

                }
                Db::table('up_order')->where('order_id',$info['order_id'])->setField('order_status',2);
                //插入财务表
                $resultAll1['revenue_name'] = $info['order_number'];
                $resultAll1['revenue_time'] = date('Y-m-d H:i:s',time());
                $resultAll1['revenue_type'] = 1;
                $resultAll1['revenue_status'] = 1;
//                    $resultAll1['revenue_price'] = $info['order_user_pay'];
                $resultAll1['revenue_glod'] = $info['order_j_glod'];
                $resultAll1['revenue_gl_id'] = $info['order_id'];
                Db::table('up_revenue')->insert($resultAll1);

                Db::commit();
                return  json(array('status'=>200,'msg'=>"操作成功"));
            }
        }catch(\Exception $e){
            Db::rollback();//回滚
            return  json(array('status'=>100,'msg'=>"操作失败"));
        }

    }

    //修改支付接口
    public function paymentChange(){
        extract(input());
        $id = input('param.id');
        $type = input('param.type');
        Db::name('order')->where('order_id',$id)->setField('order_type',$type);
        return  json(array('status'=>200,'msg'=>"操作成功"));

    }

    //商城类型
    public function shopType(){
        $type = input('param.type');//1年级  2类型  3出版社
        if($type==1){
            $res = Db::table('up_good_grade')->select();
        }elseif ($type==2){
            $res = Db::table('up_good_type')->select();
        }elseif ($type==3){
            $res = Db::table('up_good_press')->select();
        }
        return  json(array('status'=>200,'msg'=>$res));
    }

    //热门搜索
    public function hot(){
        $where['good_hot'] = 2;
        $res = Db::table('up_goods')->where($where)->field('good_id,good_name')->select();
        return  json(array('status'=>200,'msg'=>$res));
    }
}